package com.exchange.controller;

import com.exchange.dto.ProductDto;
import com.exchange.entity.Product;
import com.exchange.service.ProductService;
import com.exchange.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/products")
@CrossOrigin(origins = "*")
@PreAuthorize("hasRole('MERCHANT') or hasRole('ADMIN')")
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    @GetMapping
    public ResponseEntity<List<ProductDto>> getProductsByMerchant() {
        // Get the current user's ID from the security context
        Long currentUserId = SecurityUtils.getCurrentUserId();
        
        // In a real application, we would get the merchant ID from the user profile
        // For now, using a placeholder based on user ID
        // In a real implementation, you would fetch the merchant ID linked to this user
        Long currentMerchantId = currentUserId; // This would be looked up in a real app
        
        List<ProductDto> products = productService.findByMerchantId(currentMerchantId);
        return ResponseEntity.ok(products);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<ProductDto> getProductById(@PathVariable Long id) {
        var result = productService.findById(id);
        if (result.isPresent()) {
            return ResponseEntity.ok(result.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @PostMapping
    public ResponseEntity<ProductDto> createProduct(@RequestBody Product product) {
        // Get the current user's ID from the security context
        Long currentUserId = SecurityUtils.getCurrentUserId();
        
        // In a real application, we would get the merchant ID from the user profile
        // For now, using a placeholder based on user ID
        Long currentMerchantId = currentUserId; // This would be looked up in a real app
        
        ProductDto createdProduct = productService.createProduct(product, currentMerchantId);
        return ResponseEntity.ok(createdProduct);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<ProductDto> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        // Verify that the current user owns this product (in a real implementation)
        ProductDto result = productService.updateProduct(id, product);
        if (result != null) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
        // Verify that the current user owns this product (in a real implementation)
        boolean deleted = productService.deleteProduct(id);
        return deleted ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }
}